package com.lagou.edu.utils;

import com.lagou.edu.annotation.CustomAutowired;
import com.lagou.edu.annotation.CustomService;
import com.lagou.edu.dao.AccountDao;
import com.lagou.edu.factory.BeanFactory;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * @author 应癫
 *
 * 事务管理器类：负责手动事务的开启、提交、回滚
 */
@CustomService
public class TransactionManager {

    @CustomAutowired
    private ConnectionUtils connectionUtils;

    public void setConnectionUtils(ConnectionUtils connectionUtils) {
        this.connectionUtils = connectionUtils;
    }


    public void beginTransaction() throws SQLException {
        connectionUtils.getCurrentThreadConn().setAutoCommit(false);
    }

    // 提交事务
    public void commit() throws SQLException {
        connectionUtils.getCurrentThreadConn().commit();
    }


    // 回滚事务
    public void rollback() throws SQLException {
        connectionUtils.getCurrentThreadConn().rollback();
    }

    // 释放连接资源
    public void release() throws Exception{
        Connection conn = connectionUtils.getCurrentThreadConn();
        conn.close();
        connectionUtils.release();//从当前线程上移除Connection对象
    }

}
